package com.itheima.web;

import com.itheima.bean.ShoppingCart;
import com.itheima.common.R;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R shoppingCartList(){
        java.util.List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        //System.out.println(shoppingCart);
        ShoppingCart cartOne = shoppingCartService.add(shoppingCart);
        return R.success(cartOne);
    }

    @PostMapping("/sub")
    public R update(@RequestBody ShoppingCart shoppingCart){
        int row = shoppingCartService.update(shoppingCart);
        return row>0? R.success(1): R.error("修改失败");
    }

    @DeleteMapping("/clean")
    public R clean(){
        return R.success(shoppingCartService.delete());
    }
}
